package simple;

import test.Test;

/**
 * Description:
 *
 * @author: Faker
 * Date: 2020-11-05
 * Time: 16:23
 */
public class Code_242 {
    /**
     * 直接计算每个字符的出现次数
     * @param s
     * @param t
     * @return
     */
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()){
            return false;
        }
        int[] nums = getNums(s);
        int[] nums1 = getNums(t);
        char[] chars = s.toCharArray();
        for (int i=0;i<chars.length;i++){
            if (nums[chars[i]-'a'] != nums1[chars[i]-'a']){
                return false;
            }
        }
        return true;

    }

    public int[] getNums(String s) {
        int[] nums = new int[26];
        for(int i = 0;i<s.length();i++){
            nums[s.charAt(i) - 'a']++;
        }
        return nums;
    }

    /**
     * 一增一减，最后看是否存在不为0的计数
     * @param s
     * @param t
     * @return
     */
    public boolean isAnagram1(String s, String t) {
        if (s.length() != t.length()){
            return false;
        }
        int[] nums = new int[26];
        for(int i=0;i<s.length();i++){
            nums[s.charAt(i)-'a']++;
            nums[t.charAt(i)-'a']--;
        }
        for (int num : nums) {
            if (num != 0){
                return false;
            }
        }
        return true;

    }

    public static void main(String[] args) {
        String s = "leetcode";
        String ss = "leetcoed";
        Code_242 code_242 = new Code_242();
        System.out.println(code_242.isAnagram1(s,ss));
    }
}
